package com.pktechan.api.sendMiniMessage;

import com.alibaba.fastjson.JSONObject;
import com.pktechan.api.sendMiniMessage.enumration.SendMiniMessageEnum;
import com.pktechan.api.utils.SpringContextUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: gsPrivateSale19
 * @description: 发送消息对象初始化
 * @author: qx.zhang
 * @create: 2020-02-28 16:28
 **/
public class SendMiniMessageBeanInitialization {

    private static final Logger logger = LoggerFactory.getLogger(SendMiniMessageBeanInitialization.class);

    public static Map<SendMiniMessageEnum, SendMiniMessageStrategy> getMap() {
        logger.info("发送微信公众号模板消息通知对象初始化");

        Map<SendMiniMessageEnum, SendMiniMessageStrategy> map = new HashMap<SendMiniMessageEnum, SendMiniMessageStrategy>(SendMiniMessageEnum.values().length);

        // 首先获取 SendMessageStrategy 接口的所有的实现类
        Map<String, SendMiniMessageStrategy> clazzMap = SpringContextUtil.getApplicationContext().getBeansOfType(SendMiniMessageStrategy.class);

        for (Map.Entry<String, SendMiniMessageStrategy> entry : clazzMap.entrySet()) {
            logger.info("解析到的class文件：{}" + entry.getValue().getClass());
            SendMiniMessageStrategy sendMiniMessageStrategy = SpringContextUtil.getApplicationContext().getBean(entry.getValue().getClass());
            map.put(sendMiniMessageStrategy.getMappingEnum(), sendMiniMessageStrategy);
        }
        logger.info("解析后的sendMiniMessageStrategyMap:{}" + JSONObject.toJSONString(map));
        return map;
    }

}
